package com.dabai.magiskmodulerepair.utils;

import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.webkit.ValueCallback;
import android.webkit.WebView;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class AssetsUtils {

    private static final String TAG = "DABAI";


    /**
     * 把assets的文件复制到外边
     * @param context
     * @param assetsFileName
     * @param OutFileName
     * @throws IOException
     */
    public static void copyFile(Context context,String assetsFileName, String OutFileName) throws IOException
    {
        File f = new File(OutFileName);
        if (f.exists())
            f.delete();
        f = new File(OutFileName);
        f.createNewFile();
        InputStream I = context.getAssets().open(assetsFileName);
        OutputStream O = new FileOutputStream(OutFileName);
        byte[] b = new byte[1024];
        int l = I.read(b);
        while (l > 0)
        {
            O.write(b, 0, l);
            l = I.read(b);
        }
        O.flush();
        I.close();
        O.close();
    }


    /**
     * 加载assets内的文本文件
     * @param context
     * @param path
     * @return
     */
    public static String loadAssetsText(Context context, String path) {

        String jsStr = "";
        try {
            InputStream in = context.getAssets().open(path);
            byte[] buff = new byte[1024];
            ByteArrayOutputStream fromFile = new ByteArrayOutputStream();
            do {
                int numRead = in.read(buff);
                if (numRead <= 0) {
                    break;
                }
                fromFile.write(buff, 0, numRead);
            } while (true);
            jsStr = fromFile.toString();
            in.close();
            fromFile.close();
            return jsStr;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "null";
    }

    /**
     * webView 加载Assets内的js 注入
     * 要写在onPageStarted内
     * @param webView
     * @param path
     */
    public static void loadJS(WebView webView, String path) {

        String jsStr = "";
        try {
            InputStream in = webView.getContext().getAssets().open(path);
            byte[] buff = new byte[1024];
            ByteArrayOutputStream fromFile = new ByteArrayOutputStream();
            do {
                int numRead = in.read(buff);
                if (numRead <= 0) {
                    break;
                }
                fromFile.write(buff, 0, numRead);
            } while (true);
            jsStr = fromFile.toString();
            in.close();
            fromFile.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            webView.evaluateJavascript(jsStr, value -> {
                //Log.d(TAG, "loadJS: "+value);
            });
        }

    }


}
